<?php
class Register {
	private $message = "";
    function Register() {
    	
    }
 
    public function run() {
    	$_SESSION['alowTime'] = isset($_SESSION['alowTime'])?$_SESSION['alowTime']:time();
		if($_SESSION['alowTime']>time()) {
			return ;
		}
    	$_SESSION['rg']['is_first_enter'] = isset ($_SESSION['rg']['is_first_enter']) ? 1 : 0;
		if ($_SESSION['rg']['is_first_enter'] == 0) {
			$this->message = "准备为你的群开启索妮娅的服务吧\n".
			"首先，你得把你要开启的群的群号告诉索妮娅\n 格式为 [-c 群号](如 -c 12345678) \n退出该模式请输入 -q";
			return;
		} 
		
		if (preg_match('/^-(\S+)\s*(.*)$/is', trim($_POST[Message]), $m)) {
			$funName = "__" . $m[1];
			
				if(method_exists($this,$funName))
				{
					$this-> $funName($m);
				}else{
					$_SESSION['rg']['errcom_num'] = isset($_SESSION['rg']['errcom_num'])?$_SESSION['rg']['errcom_num']+1:0;
					if($_SESSION['rg']['errcom_num']>10) {
						$_SESSION['alowTime']=time()+120;
						$this->__q("");
					}
					$this->message = "咦、说什么呢╮(╯▽╰)╭";
				}
				
			
		} else {
					$_SESSION['rg']['errcom_num'] = isset($_SESSION['rg']['errcom_num'])?$_SESSION['rg']['errcom_num']+1:0;
					if($_SESSION['rg']['errcom_num']>10) {
						$_SESSION['alowTime']=time()+120;
						$this->__q("");
					}
					$this->message = "貌似，命令什么的格式不对啊- -";
					
					
		}
    }
    
    private  function  __c($m) {
		//echo $m[2];
		$api = new Api("IsClusterAdmin");
		$lv1 = $api->doApi(trim($m[2]), $_POST[Sender]);
		if($lv1<=0) {
			$_SESSION['rg']['errcom_num'] = isset($_SESSION['rg']['errcom_num'])?$_SESSION['rg']['errcom_num']+1:0;
					if($_SESSION['rg']['errcom_num']>10) {
						$_SESSION['alowTime']=time()+120;
						$this->__q("");
			}
			$this->message = "您好,您还不是[{$m[2]}]群的管理员,操作无效~~(如果是的话，那就请先在群里先说几句话~~！)" ;
			return ;
		}
		
		$_SESSION['rg']['clusterNum'] = trim($m[2]);
		$this->message = "很好,身份确认已经通过, 下面请你认真对待我的问题\n".
		"回答任何一个问题请在答案前面加上 -a (注意跟答案间的空格)" ."\n\n".
		"1.请问索妮娅的主人是谁?";
		$_SESSION['rg']['question'] = 0;
		$_SESSION['rg']['errTime']= 0;
		
	}
	
    private function __q($m) {
		//if($_POST[Event]=="ReceiveNormalIM")
			unset ($_SESSION['NOWMODE']);
		//else
		//	unset ($_SESSION['QUN_MODE']);
		unset ($_SESSION['rg']);
		$this->message = "退出解锁模式了哦，多多和我说话吧，喵~！";
	}
	
	private function __a($m) {
		if(!isset($_SESSION['rg']['clusterNum'])) {
			$this->message = "还没有设置好你要开启的群的群号码、、请用[-c 群号]来设置";
			return;
		}
		global $question_arr ;
		$arr = $question_arr;
		if(trim($m[2])==$arr[$_SESSION['rg']['question']]['anw']) {
			
			if($_SESSION['rg']['question']==count($arr)-1) {
				@ DB :: Query("update td_cluster_favorable set `is_service`=1  where ClusterNum='{$_SESSION['rg']['clusterNum']}'");
				
				$this->message = "您好,您已经为群[{$_SESSION['rg']['clusterNum']}]成功开启索妮娅的服务了";
				$api = new Api("SendClusterMessage");
				$api->doApi($_SESSION['rg']['clusterNum'],"{$_POST[Nick]}({$_POST[Sender]})刚刚为本群开启了索妮娅。");
				$this->__q($m);
				return ;
			}
			
			if($_SESSION['rg']['question']==7) {
				$this->message = "回答正确~! \n"."下面的问题可能对你有些不适不过还是请认真对待：\n".$arr[$_SESSION['rg']['question']+1]['que'];
			}else
				$this->message = "回答正确~! \n"."下一道：\n".$arr[$_SESSION['rg']['question']+1]['que'];
			
			
			$_SESSION['rg']['question'] = $_SESSION['rg']['question'] +1;
			$_SESSION['rg']['errTime'] = 0;
			
		}else {
			$_SESSION['rg']['errTime']=$_SESSION['rg']['errTime']+1;
			
			if($_SESSION['rg']['errTime']>2){
				$this->__q($m);
				$this->message = "很遗憾, 请你休息一会儿再来吧。。\n强制退出本模式";
				
				$_SESSION['alowTime'] = time() + 1800;
				return;
			}
			$this->message = "答案不对、每道题如果连续三次不对的话,那就、、、╮(╯▽╰)╭";
			if($arr[$_SESSION['rg']['question']]['ts']) {
				$this->message = $this->message."\n".$arr[$_SESSION['rg']['question']]['ts'];
			}
		}
		
	}

	private function __get($property_name) {
		return $this-> $property_name;
	}

	private function __set($property_name, $value) {
		$this->$property_name = $value;
	}
}
?>